package mop.xml;

import java.text.ParseException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Simple class made for parsing XML times (default class did not work).
 * 
 * @author	MOP Team 7
 */
public class TimeParser
{
	private static final String REGEX = "(\\d+)\\-(\\d+)\\-(\\d+)T(\\d+):(\\d+):(\\d+)";

	/**
	 * @param 	time
	 * 			Example: 2009-10-28T20:00:00
	 * @return	A Date object.
	 * @throws 	ParseException 
	 */
	public static Date parse(String time) 
		throws ParseException
	{
		Pattern pattern = Pattern.compile(REGEX);
		Matcher matcher = pattern.matcher(time);
		
		if (matcher == null || ! matcher.matches() || matcher.groupCount() != 6)
			throw new ParseException(time, 0);
		
		int year = Integer.parseInt(matcher.group(1));
		int month = Integer.parseInt(matcher.group(2));
		int day = Integer.parseInt(matcher.group(3));
		int hour = Integer.parseInt(matcher.group(4));
		int minute = Integer.parseInt(matcher.group(5));
		int second = Integer.parseInt(matcher.group(6));
		
		return new Date(year - 1900, month, day, hour, minute, second);
	}
}